<?php
/**
 * <b>RUBIK IT Solution & Development ltd.</b>
 *
 * PROJECT		: Auction
 * @version 	: 1.1
 * COPYRIGHT    : 2010
 * ------------------------------------------------------
 *
 * Created on	:  Sep 13, 2010
 *
 */

// no direct access
defined ( '_FW_EXEC' ) or die ( 'Restricted access' );
RClassesImporter::cls('controllers.AbstractBackEndController');
RCoreImporter::cls('html.fieldInfo.PasswordHtmlFieldInfo');
/**
 * The controller for State application
 *
 */
class MemberBackController extends AbstractBackEndController {

	var $_model;

	function __construct() {
		parent::__construct();
		SysApplication::importModelCls('MemberModel');
		$this->_model = new MemberModel();
		$this->registerTask ( "new", "edit" );
		$this->registerTask ( "apply", "save" );
	}

	function display() {
		$this->setTitle ( "Manage Member" );
		$this->addToolbarButton("export");
		$this->addToolbarButton ( "new" );
		$this->addToolbarButton ( "edit" );
		$this->addToolbarButton ( "delete" );
		$msg = SysRequest::getVar ( "msg" );
		RCoreImporter::cls( "html.TableControl" );
		RClassesImporter::cls("dbo.dtos.MemberDTO" );
		$table = new TableControl ( 'MemberDTO', NULL, $_REQUEST, 'MemberCtrl', '', ' member_id DESC' );
		//TableControl($dtoName, $arrayViewField, $arrayRequest, $name, $where, $orderBy, $db=null)
		$nextExec = $this->getExecuteBy();
		$nextExec = ucfirst($nextExec);

		$table->removeField('group_id');
		$table->removeField('member_id');
		$table->removeField('firstname');
		$table->removeField('password');
		$table->removeField('placebirth');
		$table->removeField('birthday');
		$table->removeField('city');
		$table->removeField('zipcode');
		$table->removeField('involvement');
		$table->removeField('firstname1');
		$table->removeField('lastname1');
		$table->removeField('placebirth1');
		$table->removeField('birthday1');

		$table->removeField('mobile1');
		$table->removeField('email1');
		$table->removeField('skype1');
		$table->removeField('city1');
		$table->removeField('country1');
		$table->removeField('zipcode1');
		$table->removeField('involvement1');

		$table->removeField('employer_address');
		$table->removeField('industry');
		$table->removeField('position');
		$table->removeField('looking_job');
		$table->removeField('by_member');
		$table->removeField('how_know');
		$table->removeField('how_long');
		$table->setLinkRule("username",'javascript:edit'.$nextExec.'Item($key)');
		$msg = "<p class=\"form-message\" style=\"text-align:left;\">
		{$msg}
			  </p>";
		$tblHtml = $table->createHtmlTable(20);
		eval('echo "'.SysApplication::loadTemplate('back/listMember').'";');
	}

	function edit($dto = null) {
		$this->setTitle ( "Edit" );
		$this->addToolbarButton ( "save" );
		//	$this->addToolbarButton ( "apply" );
		$this->addToolbarButton ( "cancel" );
		$memberDTO = new MemberDTO();
		$oModel = $this->_model;
		$cid = SysRequest::getVar ( "cid" );
		if (empty( $dto )) {
			if(is_array($cid)) {
				$cid = $cid[0];
			}
			$dto = $oModel->getMemberDetails($cid);
		}
		$_SESSION['c_password'] = $dto->password;
		
		
		RCoreImporter::cls( "html.FormFieldsControl" );
		$formData = new FormFieldsControl ( $dto );
		//$formData->removeField('password');
		$formData->setFieldHtmlInfo('password', $memberDTO->password);
		$formData->setFieldHtmlInfo('group_id',$this->_getNewsCbxFieldInfo('group_id'));
		$formData->setFieldHtmlInfo('birthday',$this->_getDateHtmlFieldInfo("birthday"));
		
		$formData->setFieldHtmlInfo('password', $memberDTO->password);
		$htmlContent = $formData->generateHtml();
		$msg = SysRequest::getVar('msg');
		eval('echo "'.SysApplication::loadTemplate('back/editMember').'";');
	}
	function export($dto = null) {
		RClassesImporter::cls('dbo.dtos.MemberViewDTO');
		RExtImporter::cls('phpexcel.ExcelTableControl');
		$table = new ExcelTableControl ('MemberViewDTO', NULL, $_REQUEST, 'THDTExportCtrl', $where, '');
		$table->removeField('member_id');
		//$table->removeField('datem');
		//$table->removeField('nam');
		//$table->removeField('thang');
		$table->runExport();
		exit();
	}
	function save($isMemberEdit = false) {
		$model = $this->_model;
		$dto = new MemberDTO();
		$abc = $_SESSION['c_password'];
		if (SysRequest::getInt('id')) {
			$dto->resetToNull();
		}
		$dto->bind($_POST);
		if ($_POST['password'] == $abc){
			$dto->password = null;
		}else if ($_POST['password'] != $abc){
			$dto->password = $_POST['password'];
		}
		if ($dto->checkKeyNotDefault()) {
			$dto->setEmptyNumberToZero();
		}

		$result = $model->saveMemberDTO ( $dto );
		if ($dto->hasError())
		$msg = $dto->getAllErrorsMsg();
		if ($result === false || $this->getExecuteVal () == 'apply') {
			$_REQUEST ["msg"] = $msg ? $msg : SysLanguage::_ ( "ITEM_SAVE_SUCCESSFUL" );
			return $this->edit ($dto);
		} else {
			$this->setRedirectToFunction('display', $msg ? $msg : SysLanguage::_ ( "ITEM_SAVE_SUCCESSFUL" ));
		}
	}

	function &_getNewsCbxFieldInfo($info) {
		$sql = "SELECT group_id, name FROM role";
		$info = &$this->_getSqlComboboxHtmlFieldInfo($info, $sql);
		if ($arrayOrObject) {
			$info->setValue($arrayOrObject);
		}
		return $info;
	}

	function apply($isMemberEdit = false) {
		$model = $this->_model;
		$dto = new MemberDTO();
		if (SysRequest::getInt('id')) {
			$dto->resetToNull();
		}
		$dto->bind($_POST);
		if ($dto->checkKeyNotDefault()) {
			$dto->setEmptyNumberToZero();
		}

		// files processing
		//$this->_setFileValue('icon', $dto, 'files/images/Member/thumbnail/', $_POST);

		$result = $model->saveMemberDTO ( $dto );
		if ($dto->hasError())
		$msg = $dto->getAllErrorsMsg();

		if ($result === false || $this->getExecuteVal () == 'apply') {
			$_REQUEST ["msg"] = $msg ? $msg : SysLanguage::_ ( "ITEM_SAVE_SUCCESSFUL" );
			return $this->edit ($dto);
		}
	}
	function &_getPicFileFiledInfo($inputName) {

		RCoreImporter::cls( "html.fieldInfo.FlashFileHtmlFieldInfo" );
		$field = new FlashFileHtmlFieldInfo($inputName,$linkRule);
		$field->setSupportedFilteType('.jpg,.jpeg,.bmp,.png,.gif');
		return $field;
	}

	function delete() {
		$model = $this->_model;
		$arrayIds = SysRequest::getVar ( "cid" );
		$model->delete($arrayIds);
		$this->setRedirectToFunction('display', SysLanguage::_ ( "ITEM_DELETE_SUCCESSFUL" ));
	}
}
